/**
 * Project: Useful Programs <br>
 * Package: other.dfa
 */
package org.leumasjaffe.math.dfa;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
 * Class .DFA in file DFA.java <br>
 * 
 * @author leumasjaffe
 * Created on: Sep 3, 2012
 * 
 */
@NonNullByDefault
public class DFA {
	final int Q;
	final int Sigma;
	final int[][] delta;
	final int s;
	final int[] F;
		
	public DFA(int numStates, int alphabet, int[][] delta, int startState, int[] finalStates) {
		this.Q = numStates;
		this.Sigma = alphabet;
		this.delta = delta;
		this.s = startState;
		this.F = finalStates;
	}
	
	public int delta(int q, int a) {
		return this.delta[q][a];
	}
	
	public boolean accepts(int[] x) {
		int out = deltax(x);
		for (int f : this.F)
			if (out == f)
				return true;
		return false;
	}
	
	public int deltax(int[] x) {
		return deltax(this.s, x, 0, x.length);
	}

	private int deltax(int q, int[] x, int i, int len) {
		if (i == len)
			return q;
		return deltax(delta(q, x[i]), x, i+1, len);
	}

}
